package com.itic.mobile.kpl;



import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.utils.DateHelper;
import com.itic.appbase.framework.utils.HttpRequestUtils;
import com.itic.mobile.base.BaseMobileInterface;
import com.jd.open.api.sdk.internal.util.CodecUtil;
import com.jd.open.api.sdk.internal.util.StringUtil;

import net.sf.json.JSONObject;


/**
 * 京东联盟
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/mobile/kpl")
public class KplInterface
     extends BaseMobileInterface {
	
	/**
	 * 默认的京东联盟账号v=1.0&method=jd.kpl.open.unionservice.queryCommisions&access_token=4a6b006949b846329190a5cc4251aee48&app_key=7ac94770f7574f00a401657d8c9ac40d&sign_method=md5&format=json&timestamp=2017-05-07 12:33:43&sign=3F3F99EAAD74ABCB2CCA677305353AF0&param_json={"param":{"startDate":1428310762000,"refStatus":-255,"pageNo":1,"pageSize":500,"clearStatus":-255,"unionId":1000151476,"yn":-255,"endDate":1494061162000},"sourceId":100001}
	 * 	 友惠家公司
	 * */
	private static String url = "https://router.jd.com/api";
	private static String unionId = "1000169071";
	private static String key = "2d67b6f7d09e42c28d49fa88248db03c";
	private static String secret = "4fb7e608d6c743b5a4dd05cffc8941d3";
	private static String accesstoken = "fb71924a1f174013bf85de4e961bf9558";
	
	/**
	 * 获取优惠券信息
	 *  * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getCouponUrl")
	@ResponseBody
	public String getCouponUrl( String sku ) throws Exception{
		
		String url = "https://router.jd.com/api";
		String timestamp = DateHelper.getDateTime();
		String version = "2.0";
        String method = "jd.kpl.open.item.findjoinactives";
        String signMethod = "md5";
		String format = "json";
        String accessToken = "b617625886764a3d9e797a7d6e27bc4e8";
        String appKey = "2d67b6f7d09e42c28d49fa88248db03c";
        String paramJson = "{\"areaId\":\"\",\"sku\":\""+sku+"\"}";
        String appSecret = "4fb7e608d6c743b5a4dd05cffc8941d3";
        
        StringBuffer sb = new StringBuffer();
		sb.append("v="+version);
		sb.append("&method="+method);
		sb.append("&app_key="+appKey);
		sb.append("&access_token="+accessToken);
		sb.append("&param_json="+paramJson);
		sb.append("&format=json");
		sb.append("&sign_method=md5");
		sb.append("&sign="+buildSign(timestamp,version, signMethod ,format ,  method ,paramJson , accessToken , appKey, appSecret ));
		
		String sendPost = HttpRequestUtils.sendPost(url, sb.toString());
		return sendPost;
		
	}
	
	/**
	 * 获取类目信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getQuery")
	@ResponseBody
	public String getQuery( ) throws Exception{
		
		String url = "https://router.jd.com/api";
		
		String version = "1.0";
        String method = "public.product.base.query";
        String paramJson = "{\"areaId\":\"\",\"sku\":\"21694790906\"}";
        String accessToken = "b617625886764a3d9e797a7d6e27bc4e8";
        String appKey = "2d67b6f7d09e42c28d49fa88248db03c";
		
        StringBuffer sb = new StringBuffer();
		sb.append("v="+version);
		sb.append("&method="+method);
		sb.append("&app_key="+appKey);
		sb.append("&access_token="+accessToken);
		sb.append("&param_json="+paramJson);
        
		String sendPost = HttpRequestUtils.sendPost(url, sb.toString());
		return sendPost;
	}
	
	/**
	 * 初始化京东联盟配置信息
	 * @param unionid
	 * unionid 1000466519
	 * key 758E9DE6C40A4C97C3195D1E383E41BE
	 * secret 50176e262f10453d97ce5fba2918dc3d
	 * access_token 8de91341-9d1d-4216-95bf-4e8bc7f82454
	 * position 1091396120
	 * https://router.jd.com/api?v=1.0&method=jd.kpl.open.unionservice.queryimportorders&access_token=4974e726622a4a8e906f47372a72f4c78&app_key=7ac94770f7574f00a401657d8c9ac40d&sign_method=md5&format=json&timestamp=2017-05-07 15:35:40&sign=53CAD7B2AF5E7D463346394B7C60C9E7&param_json={"param":{"startDate":"1428310762000","refStatus":-255,"pageNo":1,"pageSize":500,"clearStatus":-255,"unionId":1000151476,"yn":-255,"endDate":"1494061162000"},"sourceId":100001}
	 */
	@RequestMapping(value = "/getlist")
	@ResponseBody
	public Map<String, Object> getlist( ) throws Exception{
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try{
			//https://router.jd.com/api?v=1.0&method=jd.kpl.open.unionservice.queryimportorders&access_token=b617625886764a3d9e797a7d6e27bc4e8&app_key=2d67b6f7d09e42c28d49fa88248db03c&sign_method=md5&format=json&timestamp=2018-02-24 17:14:59&param_json={"param":{"startDate":"1519401600000","refStatus":-255,"pageNo":1,"pageSize":500,"clearStatus":-255,"unionId":1000169071,"yn":-255,"endDate":"1518192000000"},"sourceId":100001}
			//https://router.jd.com/api?v=1.0&method=jd.kpl.open.unionservice.queryimportorders&access_token=b617625886764a3d9e797a7d6e27bc4e8&app_key=2d67b6f7d09e42c28d49fa88248db03c&sign_method=md5&format=json&timestamp=2018-02-23 17:14:59&param_json={"param":{"startDate":"1519123320000","refStatus":-255,"pageNo":1,"pageSize":500,"clearStatus":-255,"unionId":1000169071,"yn":-255,"endDate":"1519987320000"},"sourceId":100001}
			//private static String KEY = "2d67b6f7d09e42c28d49fa88248db03c";
			//private static String SECRET = "4fb7e608d6c743b5a4dd05cffc8941d3";
			//private static String ACCESSTOKEN = "fb71924a1f174013bf85de4e961bf9558";
			  //String tempUrl = "https://router.jd.com/api?v=1.0&method=jd.kpl.open.unionservice.queryimportorders&access_token=b617625886764a3d9e797a7d6e27bc4e8&app_key=2d67b6f7d09e42c28d49fa88248db03c&sign_method=md5&format=json&timestamp=2018-02-23 17:14:59&param_json={\"param\":{\"startDate\":\"1519123320000\",\"refStatus\":-255,\"pageNo\":1,\"pageSize\":500,\"clearStatus\":-255,\"unionId\":1000169071,\"yn\":-255,\"endDate\":\"1519987320000\"},\"sourceId\":100001}";
			//String tempUrl = "https://router.jd.com/api?v=1.0&method=jd.kpl.open.unionservice.queryimportorders&access_token=b617625886764a3d9e797a7d6e27bc4e8&app_key=2d67b6f7d09e42c28d49fa88248db03c&sign_method=md5&format=json&timestamp=2018-02-23 17:14:59&param_json={\"param\":{\"startDate\":\"1519123320000\",\"refStatus\":-255,\"pageNo\":1,\"pageSize\":500,\"clearStatus\":-255,\"unionId\":1000169071,\"yn\":-255,\"endDate\":\"1519987320000\"},\"sourceId\":100001}";
			//tempUrl = tempUrl.replaceAll(" ", "%20");
			//https://router.jd.com/api?v=2.0&method=jd.kepler.order.getlist&access_token=835adf3d92224b1db3de3f78047d93388&app_key=7ac94770f7574f00a401657d8c9ac40d&sign_method=md5&format=json&timestamp=2017-11-29 15:43:41&sign=563A3979EDC75E7FDFCA6B02D3903EE1&param_json={"beginTime":"20160628010101","endTime":"20160628235959","pageIndex":1,"pageSize":10}
			//v=2.0&method=jd.kepler.order.getlist&access_token=835adf3d92224b1db3de3f78047d93388&app_key=7ac94770f7574f00a401657d8c9ac40d&sign_method=md5&format=json&timestamp=2017-11-29 15:43:41&sign=563A3979EDC75E7FDFCA6B02D3903EE1&param_json={"beginTime":"20160628010101","endTime":"20160628235959","pageIndex":1,"pageSize":10}
			  
			String url = "https://router.jd.com/api";
			
			String timestamp = DateHelper.getDateTime();
			String version = "2.0";
			String signMethod = "md5";
			String format = "json";
            String method = "jd.kepler.order.getlist";
            String paramJson = "{\"beginTime\":\"20180226000000\",\"endTime\":\"20180226235959\",\"pageIndex\":1,\"pageSize\":10}";
            String accessToken = "fb71924a1f174013bf85de4e961bf9558";
            String appKey = "2d67b6f7d09e42c28d49fa88248db03c";
            String appSecret = "4fb7e608d6c743b5a4dd05cffc8941d3";
			
			StringBuffer sb = new StringBuffer();
			sb.append("v="+version);
			sb.append("&method="+method);
			sb.append("&app_key="+appKey);
			sb.append("&access_token="+accessToken);
			sb.append("&timestamp="+timestamp);
			sb.append("&format=json");
			sb.append("&sign_method=md5");
			sb.append("&param_json="+paramJson);
			sb.append("&sign="+buildSign(timestamp,version, signMethod ,format ,  method ,paramJson , accessToken , appKey, appSecret ));
			
			String sendPost = HttpRequestUtils.sendPost(url, sb.toString());
			resultMap.put(DATA, sendPost);
		}catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("JdlmInterface", "getConUrl", e.toString(), "京东联盟优惠券二合一转链接", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		return resultMap;
	}
	
	
	/**
	 * 初始化京东联盟配置信息
	 * https://router.jd.com/api?v=1.0&method=jd.kpl.open.unionservice.queryCommisions&access_token=4a6b006949b846329190a5cc4251aee48&app_key=7ac94770f7574f00a401657d8c9ac40d&sign_method=md5&format=json&timestamp=2017-05-07 12:33:43&sign=3F3F99EAAD74ABCB2CCA677305353AF0&param_json={"param":{"startDate":1428310762000,"refStatus":-255,"pageNo":1,"pageSize":500,"clearStatus":-255,"unionId":1000151476,"yn":-255,"endDate":1494061162000},"sourceId":100001}
	 * @param unionid
	 * unionid 1000466519
	 * key 758E9DE6C40A4C97C3195D1E383E41BE
	 * secret 50176e262f10453d97ce5fba2918dc3d
	 * access_token 8de91341-9d1d-4216-95bf-4e8bc7f82454
	 * position 1091396120
	 * https://router.jd.com/api?v=1.0&method=jd.kpl.open.unionservice.queryimportorders&access_token=4974e726622a4a8e906f47372a72f4c78&app_key=7ac94770f7574f00a401657d8c9ac40d&sign_method=md5&format=json&timestamp=2017-05-07 15:35:40&sign=53CAD7B2AF5E7D463346394B7C60C9E7&param_json={"param":{"startDate":"1428310762000","refStatus":-255,"pageNo":1,"pageSize":500,"clearStatus":-255,"unionId":1000151476,"yn":-255,"endDate":"1494061162000"},"sourceId":100001}
	 */
	@RequestMapping(value = "/queryimportorders")
	@ResponseBody
	public Map<String, Object> queryimportorders( ) throws Exception{
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try{
			//https://router.jd.com/api?v=1.0&method=jd.kpl.open.unionservice.queryimportorders&access_token=b617625886764a3d9e797a7d6e27bc4e8&app_key=2d67b6f7d09e42c28d49fa88248db03c&sign_method=md5&format=json&timestamp=2018-02-24 17:14:59&param_json={"param":{"startDate":"1519401600000","refStatus":-255,"pageNo":1,"pageSize":500,"clearStatus":-255,"unionId":1000169071,"yn":-255,"endDate":"1518192000000"},"sourceId":100001}
			//https://router.jd.com/api?v=1.0&method=jd.kpl.open.unionservice.queryimportorders&access_token=b617625886764a3d9e797a7d6e27bc4e8&app_key=2d67b6f7d09e42c28d49fa88248db03c&sign_method=md5&format=json&timestamp=2018-02-23 17:14:59&param_json={"param":{"startDate":"1519123320000","refStatus":-255,"pageNo":1,"pageSize":500,"clearStatus":-255,"unionId":1000169071,"yn":-255,"endDate":"1519987320000"},"sourceId":100001}
			
			//String tempUrl = "https://router.jd.com/api?v=1.0&method=jd.kpl.open.unionservice.queryimportorders&access_token=b617625886764a3d9e797a7d6e27bc4e8&app_key=2d67b6f7d09e42c28d49fa88248db03c&sign_method=md5&format=json&timestamp=2018-02-23 17:14:59&param_json={\"param\":{\"startDate\":\"1519123320000\",\"refStatus\":-255,\"pageNo\":1,\"pageSize\":500,\"clearStatus\":-255,\"unionId\":1000169071,\"yn\":-255,\"endDate\":\"1519987320000\"},\"sourceId\":100001}";
			//String tempUrl = "https://router.jd.com/api?v=1.0&method=jd.kpl.open.unionservice.queryimportorders&access_token=b617625886764a3d9e797a7d6e27bc4e8&app_key=2d67b6f7d09e42c28d49fa88248db03c&sign_method=md5&format=json&timestamp=2018-02-23 17:14:59&param_json={\"param\":{\"startDate\":\"1519123320000\",\"refStatus\":-255,\"pageNo\":1,\"pageSize\":500,\"clearStatus\":-255,\"unionId\":1000169071,\"yn\":-255,\"endDate\":\"1519987320000\"},\"sourceId\":100001}";
			//tempUrl = tempUrl.replaceAll(" ", "%20");
			//https://router.jd.com/api?v=2.0&method=jd.kepler.order.getlist&access_token=835adf3d92224b1db3de3f78047d93388&app_key=7ac94770f7574f00a401657d8c9ac40d&sign_method=md5&format=json&timestamp=2017-11-29 15:43:41&sign=563A3979EDC75E7FDFCA6B02D3903EE1&param_json={"beginTime":"20160628010101","endTime":"20160628235959","pageIndex":1,"pageSize":10}
			
			String url = "https://router.jd.com/api";
			
			String timestamp = DateHelper.getDateTime();
			String version = "1.0";
			String signMethod = "md5";
			String format = "json";
            String method = "jd.kpl.open.unionservice.queryimportorders";
            String paramJson = "{\"param\":{\"startDate\":\"1519574400000\",\"refStatus\":-255,\"pageNo\":1,\"pageSize\":500,\"clearStatus\":-255,\"unionId\":1000169071,\"yn\":-255,\"endDate\":\"1519660799000\"},\"sourceId\":100001}";
            String accessToken = "b617625886764a3d9e797a7d6e27bc4e8";
            String appKey = "2d67b6f7d09e42c28d49fa88248db03c";
            String appSecret = "";
			
			StringBuffer sb = new StringBuffer();
			sb.append("v="+version);
			sb.append("&method="+method);
			sb.append("&access_token="+accessToken);
			sb.append("&app_key="+appKey);
			sb.append("&sign_method=md5&format=json");
			sb.append("&timestamp="+timestamp);
			sb.append("&param_json="+paramJson);  
			//sb.append("&sign="+buildSign(timestamp,version, signMethod ,format ,  method ,paramJson , accessToken , appKey, null));
			
			String sendPost = HttpRequestUtils.sendPost(url, sb.toString());
			resultMap.put(DATA, sendPost);
		}catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("JdlmInterface", "getConUrl", e.toString(), "京东联盟优惠券二合一转链接", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		return resultMap;
	}
	
	/**
	 * 开普勒业绩订单https://router.jd.com/api?v=1.0&method=jd.kpl.open.unionservice.queryCommisions&access_token=4a6b006949b846329190a5cc4251aee48&app_key=7ac94770f7574f00a401657d8c9ac40d&sign_method=md5&format=json&timestamp=2017-05-07 12:33:43&sign=3F3F99EAAD74ABCB2CCA677305353AF0&param_json={"param":{"startDate":1428310762000,"refStatus":-255,"pageNo":1,"pageSize":500,"clearStatus":-255,"unionId":1000151476,"yn":-255,"endDate":1494061162000},"sourceId":100001}
	 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryCommisions")
	@ResponseBody
	public String queryCommisions( String orderIds, String startTime, String endTime ) throws Exception{
		String result = "";
		try{
			//https://router.jd.com/api?v=1.0&method=jd.kpl.open.unionservice.queryimportorders&access_token=b617625886764a3d9e797a7d6e27bc4e8&app_key=2d67b6f7d09e42c28d49fa88248db03c&sign_method=md5&format=json&timestamp=2018-02-24 17:14:59&param_json={"param":{"startDate":"1519401600000","refStatus":-255,"pageNo":1,"pageSize":500,"clearStatus":-255,"unionId":1000169071,"yn":-255,"endDate":"1518192000000"},"sourceId":100001}
			//https://router.jd.com/api?v=1.0&method=jd.kpl.open.unionservice.queryimportorders&access_token=b617625886764a3d9e797a7d6e27bc4e8&app_key=2d67b6f7d09e42c28d49fa88248db03c&sign_method=md5&format=json&timestamp=2018-02-23 17:14:59&param_json={"param":{"startDate":"1519123320000","refStatus":-255,"pageNo":1,"pageSize":500,"clearStatus":-255,"unionId":1000169071,"yn":-255,"endDate":"1519987320000"},"sourceId":100001}
			
			//String tempUrl = "https://router.jd.com/api?v=1.0&method=jd.kpl.open.unionservice.queryimportorders&access_token=b617625886764a3d9e797a7d6e27bc4e8&app_key=2d67b6f7d09e42c28d49fa88248db03c&sign_method=md5&format=json&timestamp=2018-02-23 17:14:59&param_json={\"param\":{\"startDate\":\"1519123320000\",\"refStatus\":-255,\"pageNo\":1,\"pageSize\":500,\"clearStatus\":-255,\"unionId\":1000169071,\"yn\":-255,\"endDate\":\"1519987320000\"},\"sourceId\":100001}";
			//String tempUrl = "https://router.jd.com/api?v=1.0&method=jd.kpl.open.unionservice.queryimportorders&access_token=b617625886764a3d9e797a7d6e27bc4e8&app_key=2d67b6f7d09e42c28d49fa88248db03c&sign_method=md5&format=json&timestamp=2018-02-23 17:14:59&param_json={\"param\":{\"startDate\":\"1519123320000\",\"refStatus\":-255,\"pageNo\":1,\"pageSize\":500,\"clearStatus\":-255,\"unionId\":1000169071,\"yn\":-255,\"endDate\":\"1519987320000\"},\"sourceId\":100001}";
			//tempUrl = tempUrl.replaceAll(" ", "%20");
			//https://router.jd.com/api?v=2.0&method=jd.kepler.order.getlist&access_token=835adf3d92224b1db3de3f78047d93388&app_key=7ac94770f7574f00a401657d8c9ac40d&sign_method=md5&format=json&timestamp=2017-11-29 15:43:41&sign=563A3979EDC75E7FDFCA6B02D3903EE1&param_json={"beginTime":"20160628010101","endTime":"20160628235959","pageIndex":1,"pageSize":10}
			//String startTime = "2017-12-01 00:00:00";
			//String endTime = "2017-12-30 23:59:59";
			
			
			
			Long startLong = DateHelper.getDateByStringDate(startTime+" 00:00:00").getTime();
			Long endLong = DateHelper.getDateByStringDate(endTime+" 23:59:59").getTime();
			String[] split = orderIds.split(",");
			List<Long> list = new ArrayList<Long>();
			if( split.length>0 ){
				for( int i=0; i<split.length;i++ ){
					String id = split[i];
					list.add(Long.parseLong(id));
				}
			}
			
			String url = "https://router.jd.com/api";
			
			String timestamp = DateHelper.getDateTime();
			String version = "1.0";
			//String signMethod = "md5";
			//String format = "json";
            String method = "jd.kpl.open.unionservice.queryCommisions";
            String paramJson = "{\"pageNo\":1,\"pageSize\":500,\"param\":{\"unionId\":1000169071,\"startDate\":\""+startLong+"\",\"endDate\":\""+endLong+"\",\"orderIds\":"+list+",\"refStatus\":-255,\"clearStatus\":-255,\"yn\":-255},\"sourceId\":100001}";
            String accessToken = "fb71924a1f174013bf85de4e961bf9558";
            String appKey = "2d67b6f7d09e42c28d49fa88248db03c";
            //String appSecret = "";
			
			StringBuffer sb = new StringBuffer();
			sb.append("v="+version);
			sb.append("&method="+method);
			sb.append("&access_token="+accessToken);
			sb.append("&app_key="+appKey);
			sb.append("&sign_method=md5&format=json");
			sb.append("&timestamp="+timestamp);
			sb.append("&param_json="+paramJson);  
			//sb.append("&sign="+buildSign(timestamp,version, signMethod ,format ,  method ,paramJson , accessToken , appKey, null));
			
			result = HttpRequestUtils.sendPost(url, sb.toString());
		}catch (Exception e) {
			logException("JdlmInterface", "getConUrl", e.toString(), "京东联盟优惠券二合一转链接", e);
		}
		return result;
	}
	
	
	/**
	 *https://router.jd.com/api?v=2.0&method=jd.kpl.open.item.findjoinactives&access_token=647bd0ca32264c64ba698b8c6eefafe98&app_key=7ac94770f7574f00a401657d8c9ac40d&sign_method=md5&format=json&timestamp=2018-01-16 17:38:28&sign=826EADEBBFF773140122E6505ECEA61E&param_json={"uid":"","sku":4461470}
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryCoupon")
	@ResponseBody
	public String queryCoupon( String sku ) throws Exception{
		String result = "";
		try{
			
			String url = "https://router.jd.com/api";
			
			String timestamp = DateHelper.getDateTime();
			String version = "2.0";
			String signMethod = "md5";
			String format = "json";
            String method = "jd.kpl.open.item.findjoinactives";
            String paramJson = "{\"uid\":\"\",\"sku\":"+sku+"}";
            String accessToken = accesstoken;
            String appKey = key;
            String appSecret = secret;
			
			StringBuffer sb = new StringBuffer();
			sb.append("v="+version);
			sb.append("&method="+method);
			sb.append("&app_key="+appKey);
			sb.append("&access_token="+accessToken);
			sb.append("&timestamp="+timestamp);
			sb.append("&format=json");
			sb.append("&sign_method=md5");
			sb.append("&param_json="+paramJson);
			sb.append("&sign="+buildSign(timestamp,version, signMethod ,format ,  method ,paramJson , accessToken , appKey, appSecret ));
			
			result = HttpRequestUtils.sendPost(url, sb.toString());
		}catch (Exception e) {
			logException("JdlmInterface", "getConUrl", e.toString(), "京东联盟优惠券二合一转链接", e);
		}
		return result;
	}
	
	public static void main(String[] args) {
		Date startTime = DateHelper.getDateByStringDate("2018-02-10 00:00:00");
		System.out.println(startTime.getTime());
		Date endTime = DateHelper.getDateByStringDate("2018-02-10 23:59:59");
		System.out.println(endTime.getTime());
	}
	
	 private String buildSign(String timestamp, String version, String signMethod ,String format , 

             String method , String paramJson , String accessToken ,String appKey, String appSecret) 

            throws Exception {

	      //第一步，按照顺序填充参数
		 
	      Map<String, String> map = new TreeMap();
	
	      map.put("timestamp", timestamp);
	
	      map.put("v", version);
	
	      map.put("sign_method", signMethod);
	
	      map.put("format", format);
	
	      map.put("method", method);
	
	      //param_json为空的时候需要写成 "{}"
	
	      map.put("param_json", paramJson);
	
	      map.put("access_token", accessToken);
	
	      map.put("app_key", appKey);
	
	
	
	      StringBuilder sb = new StringBuilder(appSecret);
	
	      //按照规则拼成字符串
	
	      for (Map.Entry entry : map.entrySet()) {
	
	          String name = (String) entry.getKey();
	
	          String value = (String) entry.getValue();
	
	          //检测参数是否为空
	
	          if (StringUtil.areNotEmpty(new String[]{name, value})) {
	
	              sb.append(name).append(value);
	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
	          }
	
	      }
	
	
	
	      sb.append(appSecret);
	
	      //MD5
	      return CodecUtil.md5(sb.toString());

	 }



	public static String md5(String source)
	
	        throws Exception
	
	{
	
	    MessageDigest md = MessageDigest.getInstance("MD5");
	
	    byte[] bytes = md.digest(source.getBytes("utf-8"));
	
	    return byte2hex(bytes);
	
	}
	
	
	
	private static String byte2hex(byte[] bytes) {
	
	    StringBuilder sign = new StringBuilder();
	
	    for (int i = 0; i < bytes.length; i++) {
	
	        String hex = Integer.toHexString(bytes[i] & 0xFF);
	
	        if (hex.length() == 1) {
	
	            sign.append("0");
	
	        }
	
	        sign.append(hex.toUpperCase());
	
	    }
	
	    return sign.toString();
	
	}
	
	
	
	public static boolean areNotEmpty(String[] values) {
	
	    boolean result = true;
	
	    if ((values == null) || (values.length == 0))
	
	        result = false;
	
	    else {
	
	        for (String value : values) {
	
	            result &= !isEmpty(value);
	
	        }
	
	    }
	
	    return result;
	
	}
	
	
	
	public static boolean isEmpty(String value) {
	
	    int strLen;
	
	    if ((value == null) || ((strLen = value.length()) == 0))
	
	        return true;
	
	
	
	    for (int i = 0; i < strLen; i++) {
	
	        if (!Character.isWhitespace(value.charAt(i))) {
	
	            return false;
	
	        }
	
	    }
	
	    return true;
	
	}


	
}